package com.arcaneshift.model.validators;

import com.arcaneshift.model.abstracts.AbstractGameRequest;
import com.arcaneshift.security.errors.GameError;
import com.arcaneshift.util.GameCacheContainer;
import com.common.model.abstracts.AbstractRequest;
import com.common.model.abstracts.AbstractValidator;
import com.common.security.abstracts.AbstractError;

public class GameValidator extends AbstractValidator {

	@Override
	public boolean validate(AbstractRequest abstractRequest) {
		AbstractGameRequest request = null;
		
		if (abstractRequest instanceof AbstractGameRequest) {
			request = (AbstractGameRequest) abstractRequest;
		} else {
			error = new GameError(AbstractError.INVALID_REQUEST);
			return false;
		}
		
		String gameId = request.getGameId();
		
		if (gameId == null || gameId.length() == 0) {
			error = new GameError(GameError.NO_GAME_PROVIDED);
			return false;
		}
		
		if (!GameCacheContainer.containsAndRefresh(gameId)) {
			error = new GameError(GameError.GAME_NOT_FOUND);
			return false;
		}
		
		return true;
	}

}
